package com.playball.lottery.frm.yhzx;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.playball.lottery.ApplicationData;
import com.playball.lottery.R;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.nt.GetJsonData;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.util.ConvertJsonToList;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MySetYongJin extends Activity {

	TextView tv_myyj_jz2x1,tv_myyj_jl,tv_myyj_jz,tv_myyj_6,tv_myyj_11x5,tv_myyj_11dj,tv_myyj_3d;
	EditText et_myyj_jz,et_myyj_jl,et_myyj_6,et_myyj_11x5,et_myyj_11dj,et_myyj_3d;
	Button btn_myyj_OK,btn_myyj_open,btn_myyj_recharge;
	private MySetYongJin context;
	
	String userid;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_my_set_yj);
		this.context = this;
		titleBar();
		tv_myyj_jl=(TextView) findViewById(R.id.tv_myyj_jl);
		tv_myyj_jz=(TextView) findViewById(R.id.tv_myyj_jz);
		tv_myyj_6=(TextView) findViewById(R.id.tv_myyj_6);
		tv_myyj_11x5=(TextView) findViewById(R.id.tv_myyj_11x5);
		tv_myyj_11dj=(TextView) findViewById(R.id.tv_myyj_11dj);
		tv_myyj_3d=(TextView) findViewById(R.id.tv_myyj_3d);
		
		et_myyj_jz=(EditText) findViewById(R.id.et_myyj_jz);
		et_myyj_jl=(EditText) findViewById(R.id.et_myyj_jl);
		et_myyj_6=(EditText) findViewById(R.id.et_myyj_6);
		et_myyj_11x5=(EditText) findViewById(R.id.et_myyj_11x5);
		et_myyj_11dj=(EditText) findViewById(R.id.et_myyj_11dj);
		et_myyj_3d=(EditText) findViewById(R.id.et_myyj_3d);
		
		btn_myyj_OK = (Button) findViewById(R.id.btn_myyj_OK);
		btn_myyj_recharge = (Button) findViewById(R.id.btn_myyj_recharge);
		btn_myyj_open = (Button) findViewById(R.id.btn_myyj_open);
		
		userid = getIntent().getExtras().getString("userId");
		
		AsyncLoader task = new AsyncLoader();
		String[] dyn = new String[2];
		JSONObject params = new JSONObject();
		try {
			params.put("userid", userid);
		} catch (JSONException ex) {
			Log.e("error", ex.toString());
		}
		dyn[0] = params.toString();
		dyn[1] = "119";
		task.execute(dyn);
		
		btn_myyj_open.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				btn_myyj_open.setSelected(!btn_myyj_open.isSelected());
			}
		});
		btn_myyj_recharge.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent1= new Intent();        
			    intent1.setAction("android.intent.action.VIEW");    
			    Uri content_url = Uri.parse(GetJsonData.urlPath_recharge+userid);   
			    intent1.setData(content_url);  
			    startActivity(intent1);
			}
		});
		btn_myyj_OK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(et_myyj_11dj.getText().toString().trim().isEmpty()
						||et_myyj_11x5.getText().toString().trim().isEmpty()
						||et_myyj_3d.getText().toString().trim().isEmpty()
						||et_myyj_6.getText().toString().trim().isEmpty()
						||et_myyj_jz.getText().toString().trim().isEmpty()
						||et_myyj_jl.getText().toString().trim().isEmpty()){
					Toast.makeText(MySetYongJin.this, "佣金不能为空", Toast.LENGTH_LONG).show();
					return;
				}
				
				AsyncLoaderSubmit task = new AsyncLoaderSubmit();
				String[] dyn = new String[2];
				JSONObject params = new JSONObject();
				try {
//					"1*0.00*0.00*0.00*1*胜负彩|2*0.00*0.00*0.00*2*四场进球彩|3*0.00*0.00*0.00*3*七星彩|4*0.00*0.00*0.00*5*双色球|5*0.00*0.00*0.00*6*福彩3D|6*0.00*0.00*0.00*13*七乐彩|7*0.00*0.00*0.00*15*六场半全场|8*0.00*0.00*0.00*19*任选九|9*0.00*0.00*0.00*28*重庆时时彩|10*0.00*0.00*0.00*39*超级大乐透|11*0.00*0.00*0.00*45*足彩单场|12*0.00*0.00*0.00*62*十一运夺金|13*0.00*0.00*0.00*63*排列3|14*0.00*0.00*0.00*64*排列5|15*0.00*0.00*0.00*73*11选5|16*0.00*0.00*0.00*90*新11选5|17*0.00*0.00*0.00*90*江苏快3|18*0.00*0.00*0.00*90*六合彩|19*0.00*0.00*0.00*91*竞足其他|20*0.00*0.00*0.00*91*竞足单关|21*0.00*0.00*0.00*91*竞足2串1"
					String message="";
					for(int i=0;i<netDataList.size();i++){
						Map<String, Object> item = netDataList.get(i);
						if(item.get("LottName").toString().equals("福彩3D")){
							message = message
									+item.get("Index").toString()+"*"
									+item.get("Scale").toString()+"*"
									+et_myyj_3d.getText().toString()+"*"
									+item.get("RmdScale").toString()+"*"
									+item.get("LottID").toString()+"*"
									+item.get("LottName").toString()+"|";
						}else if(item.get("LottName").toString().equals("十一运夺金")){
							message = message
									+item.get("Index").toString()+"*"
									+item.get("Scale").toString()+"*"
									+et_myyj_11dj.getText().toString()+"*"
									+item.get("RmdScale").toString()+"*"
									+item.get("LottID").toString()+"*"
									+item.get("LottName").toString()+"|";
						}else if(item.get("LottName").toString().equals("11选5")){
							message = message
									+item.get("Index").toString()+"*"
									+item.get("Scale").toString()+"*"
									+et_myyj_11x5.getText().toString()+"*"
									+item.get("RmdScale").toString()+"*"
									+item.get("LottID").toString()+"*"
									+item.get("LottName").toString()+"|";
						}else if(item.get("LottName").toString().equals("六合彩")){
							message = message
									+item.get("Index").toString()+"*"
									+item.get("Scale").toString()+"*"
									+et_myyj_6.getText().toString()+"*"
									+item.get("RmdScale").toString()+"*"
									+item.get("LottID").toString()+"*"
									+item.get("LottName").toString()+"|";
						}else if(item.get("LottName").toString().equals("竞足")){
							message = message
									+item.get("Index").toString()+"*"
									+item.get("Scale").toString()+"*"
									+et_myyj_jz.getText().toString()+"*"
									+item.get("RmdScale").toString()+"*"
									+item.get("LottID").toString()+"*"
									+item.get("LottName").toString()+"|";
						}else if(item.get("LottName").toString().equals("竞篮")){
							message = message
									+item.get("Index").toString()+"*"
									+item.get("Scale").toString()+"*"
									+et_myyj_jl.getText().toString()+"*"
									+item.get("RmdScale").toString()+"*"
									+item.get("LottID").toString()+"*"
									+item.get("LottName").toString()+"|";
						}
					}
					
					message = message.substring(0, message.length()-1);
					params.put("userid", userid);
					params.put("message", message);
					params.put("setpro", btn_myyj_open.isSelected());
				} catch (JSONException ex) {
					Log.e("error", ex.toString());
				}
				dyn[0] = params.toString();
				dyn[1] = "120";
				task.execute(dyn);
			}
		});
		
	}
	
	class AsyncLoaderSubmit extends AsyncTask<String, Integer, String> {// 继承AsyncTask
		String action;
		ShowTipDialog showdialog = new ShowTipDialog();
		// 1.准备运行
		protected void onPreExecute() {
			showdialog.showDialog(MySetYongJin.this);
		}
		// 2.正在后台运行
		@Override
		protected String doInBackground(String... paramsArr) {
			// 处理后台执行的任务，在后台线程执行
			String strResult = null;
			action = paramsArr[1];
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(paramsArr[0], UserID, action, getApplicationContext());
			// 获取请求返回值
			return strResult;
		}
		// 3.进度更新
		protected void onProgressUpdate(Integer... progress) {
		}
		// 4.完成后台任务
		protected void onPostExecute(String result) {
			if (result != null) {
				try {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (Integer.parseInt(js.getString("Code")) == 0) {
						Toast.makeText(MySetYongJin.this, "设置成功", Toast.LENGTH_SHORT).show();
						MySetYongJin.this.finish();
					} else {
						Toast.makeText(MySetYongJin.this, js.getString("Data"), Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException ex) {
					// 异常处理代码
					Log.e("error", ex.toString());
				} finally {
					showdialog.closeDialog();
				}
			} else {
				Toast.makeText(MySetYongJin.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}
		}
		protected void onCancelled() {
		}
	}
	
	class AsyncLoader extends AsyncTask<String, Integer, String> {// 继承AsyncTask
		String action;
		ShowTipDialog showdialog = new ShowTipDialog();
		// 1.准备运行
		protected void onPreExecute() {
			showdialog.showDialog(MySetYongJin.this);
		}
		// 2.正在后台运行
		@Override
		protected String doInBackground(String... paramsArr) {
			// 处理后台执行的任务，在后台线程执行
			String strResult = null;
			action = paramsArr[1];
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(paramsArr[0], UserID, action, getApplicationContext());
			// 获取请求返回值
			return strResult;
		}
		// 3.进度更新
		protected void onProgressUpdate(Integer... progress) {
		}
		// 4.完成后台任务
		protected void onPostExecute(String result) {
			if (result != null) {
				try {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (Integer.parseInt(js.getString("Code")) == 0) {
						try {
							JSONTokener jsonParser1 = new JSONTokener(result);
							JSONObject js1 = (JSONObject) jsonParser1.nextValue();
							if (js1.getString("Data") != null && !"".equals(js1.getString("Data"))) {
								List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
								btn_myyj_open.setSelected(Boolean.parseBoolean(ConvertJsonToList.getMap(
										js1.getString("Data")).get("IsUnion").toString()));
								list = ConvertJsonToList.getList(String.valueOf(ConvertJsonToList.getMap(
										js1.getString("Data")).get("Union")));
								netDataList = new ArrayList<Map<String, Object>>(list);
								for(Map<String, Object> item:list){
									if(Boolean.parseBoolean(ConvertJsonToList.getMap(
											js1.getString("Data")).get("IsUnion").toString()) == true){
										if(item.get("LottName").toString().equals("福彩3D")){
											et_myyj_3d.setText(item.get("Scale").toString());
											tv_myyj_3d.setText("自身佣金"+item.get("RmdScale").toString()+"%");
										}else if(item.get("LottName").toString().equals("十一运夺金")){
											et_myyj_11dj.setText(item.get("Scale").toString());
											tv_myyj_11dj.setText("自身佣金"+item.get("RmdScale").toString()+"%");
										}else if(item.get("LottName").toString().equals("11选5")){
											et_myyj_11x5.setText(item.get("Scale").toString());
											tv_myyj_11x5.setText("自身佣金"+item.get("RmdScale").toString()+"%");
										}else if(item.get("LottName").toString().equals("六合彩")){
											et_myyj_6.setText(item.get("Scale").toString());
											tv_myyj_6.setText("自身佣金"+item.get("RmdScale").toString()+"%");
										}else if(item.get("LottName").toString().equals("竞足")){
											et_myyj_jz.setText(item.get("Scale").toString());
											tv_myyj_jz.setText("自身佣金"+item.get("RmdScale").toString()+"%");
										}else if(item.get("LottName").toString().equals("竞篮")){
											et_myyj_jl.setText(item.get("Scale").toString());
											tv_myyj_jl.setText("自身佣金"+item.get("RmdScale").toString()+"%");
										}
									}else{
										if(Float.parseFloat(item.get("RmdScale").toString())-2>0){
											if(item.get("LottName").toString().equals("福彩3D")){
												et_myyj_3d.setText(""+(Float.parseFloat(item.get("RmdScale").toString())-2));
												tv_myyj_3d.setText("自身佣金"+item.get("RmdScale").toString()+"%");
											}else if(item.get("LottName").toString().equals("十一运夺金")){
												et_myyj_11dj.setText(""+(Float.parseFloat(item.get("RmdScale").toString())-2));
												tv_myyj_11dj.setText("自身佣金"+item.get("RmdScale").toString()+"%");
											}else if(item.get("LottName").toString().equals("11选5")){
												et_myyj_11x5.setText(""+(Float.parseFloat(item.get("RmdScale").toString())-2));
												tv_myyj_11x5.setText("自身佣金"+item.get("RmdScale").toString()+"%");
											}else if(item.get("LottName").toString().equals("六合彩")){
												et_myyj_6.setText(""+(Float.parseFloat(item.get("RmdScale").toString())-2));
												tv_myyj_6.setText("自身佣金"+item.get("RmdScale").toString()+"%");
											}else if(item.get("LottName").toString().equals("竞足")){
												et_myyj_jz.setText(""+(Float.parseFloat(item.get("RmdScale").toString())-2));
												tv_myyj_jz.setText("自身佣金"+item.get("RmdScale").toString()+"%");
											}else if(item.get("LottName").toString().equals("竞篮")){
												et_myyj_jl.setText(""+(Float.parseFloat(item.get("RmdScale").toString())-2));
												tv_myyj_jl.setText("自身佣金"+item.get("RmdScale").toString()+"%");
											}
										}else{
											if(item.get("LottName").toString().equals("福彩3D")){
												et_myyj_3d.setText(item.get("Scale").toString());
												tv_myyj_3d.setText("自身佣金"+item.get("RmdScale").toString()+"%");
											}else if(item.get("LottName").toString().equals("十一运夺金")){
												et_myyj_11dj.setText(item.get("Scale").toString());
												tv_myyj_11dj.setText("自身佣金"+item.get("RmdScale").toString()+"%");
											}else if(item.get("LottName").toString().equals("11选5")){
												et_myyj_11x5.setText(item.get("Scale").toString());
												tv_myyj_11x5.setText("自身佣金"+item.get("RmdScale").toString()+"%");
											}else if(item.get("LottName").toString().equals("六合彩")){
												et_myyj_6.setText(item.get("Scale").toString());
												tv_myyj_6.setText("自身佣金"+item.get("RmdScale").toString()+"%");
											}else if(item.get("LottName").toString().equals("竞足")){
												et_myyj_jz.setText(item.get("Scale").toString());
												tv_myyj_jz.setText("自身佣金"+item.get("RmdScale").toString()+"%");
											}else if(item.get("LottName").toString().equals("竞篮")){
												et_myyj_jl.setText(item.get("Scale").toString());
												tv_myyj_jl.setText("自身佣金"+item.get("RmdScale").toString()+"%");
											}
										}
									}
								}
							} else {
								// 数据为空时为0
							}
						} catch (JSONException e) {
							// 异常处理代码
							Log.e("error", e.toString());
						}
						// 显示
					} else {
						Toast.makeText(MySetYongJin.this, js.getString("Data"), Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException ex) {
					// 异常处理代码
					Log.e("error", ex.toString());
				} finally {
					showdialog.closeDialog();
				}
			} else {
				Toast.makeText(MySetYongJin.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}
		}
		protected void onCancelled() {
		}
	}
	
	
	private List<Map<String, Object>> netDataList;
	
	// TitleBar标题栏
	public void titleBar() {
		TextView info = (TextView) findViewById(R.id.top_title_info);
		info.setText("设置佣金");
		TextView back = (TextView) findViewById(R.id.top_title_back);
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				MySetYongJin.this.finish();
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
				this.finish();
				return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}

	};
}
